﻿using OperationPlugins.Handlers;

namespace OperationPlugins.Models
{
    [HandlerOf(typeof(SetPropertyValueEnvelope), Inherited = false)]
    public abstract class SetPropertyValueHandler : ContextualPlugin, IHandler
    {
        /// <summary>
        /// Handles an object.
        /// </summary>
        /// <param name="value">The object to be handled.</param>
        /// <param name="context">The operation context.</param>
        void IHandler.Handle(object value, OperationContext context)
        {
            var envelope = (SetPropertyValueEnvelope)value;

            Value = envelope;

            Context = context;

            Set(envelope.Model, envelope.NewValue, envelope.OldValue);
        }

        /// <summary>
        /// Set property value.
        /// </summary>
        /// <param name="model">The model object.</param>
        /// <param name="newValue">The new value of the property that changed.</param>
        /// <param name="oldValue">The old value of the property that changed.</param>
        protected abstract void Set(object model, PropertyValue newValue, PropertyValue oldValue);

    }
}
